/*
 * Copyright (C) 2013 Winson Wong
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package myidea.winson.android.cangjieime;

import java.util.ArrayList;

import android.R.integer;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

public class Preferences implements SharedPreferences.OnSharedPreferenceChangeListener{
	
    static final String PREF_DEFAULT_PICKUP_MODE = "Default_Pickup_Mode";
    static final String PREF_ENABLE_KEY_SOUND = "Enable_Key_Sound";
    static final String PREF_ENABLE_KEY_VIBRATE = "Enable_Key_Vibrate";
    static final String PREF_ENABLE_RELATED_CHINESE_WORD = "Enable_Related_Chinese_Word";
    static final String PREF_ENABLE_CANGJIE_AUTO_CORRECT = "Enable_Cangjie_Auto_Correct";
    static final String PREF_IS_SYMBOLS_PICKUP_CHINESE = "Is_Symbols_Pickup_Chinese";
    static final String PREF_SUPPORT_CANGJIE_THIRD = "Support_Cangjie_Third";
    static final String PREF_TRADITION_TO_SIMPLIFIED = "Is_Tradition_To_Simplified";
    static final String PREF_FORCES_RUN_FULLSCREEN = "Forces_Run_Fullscreen";
    static final String PREF_KEYBOARD_FIVE_ROW_MODE = "Keyboard_Five_Row_Mode";
    static final String PREF_KEYBOARD_KEY_SIZE_MODE = "Keyboard_Key_Size_Mode";
    static final String PREF_MIX_INPUT_DISPLAY_MODE = "Mix_Input_Display_Mode";
    static final String PREF_CANGJIE_INPUT_KEY_CODE_POSITION = "Cangjie_Input_Key_Code_Position";
    
    static final int DPM_ENGLISH_FIRST = 0;
    static final int DPM_CHINESE_FIRST = 1;
    static final int DPM_INTELLIGENT_ENGLISH_FIRST = 2;
    static final int DPM_INTELLIGENT_CHINESE_FIRST = 3;
    
    static final int FRF_SYSTEM = 0;
    static final int FRF_NO = 1;
    static final int FRF_YES = 2;
    
    static final int MID_ENGLISH_FIRST = 0;
    static final int MID_DEFAULT_FIRST = 1;
    static final int MID_DEFAULT_FIRST_WITHOUT_CANDIDATE = 2;
    
    static final int CIKCDP_NO_SHOW = 0;
    static final int CIKCDP_LEFT = 1;
    static final int CIKCDP_MIDDLE = 2;
    static final int CIKCDP_RIGHT = 3;
    
    static Preferences currentPrefObj = null;
    
	private SharedPreferences mPref;
	
	private ArrayList<IKeyboardPrefChangedListener> mKeyboardPrefChangedListeners = new ArrayList<IKeyboardPrefChangedListener>();
	
	int defaultPickupMode;
	boolean enableKeySound;
	boolean enableKeyVibrate;
	boolean enableRelatedChineseWord;
	boolean enableCangjieAutoCorrect;
	boolean isSymbolsPickupChinese;
	boolean isSupportCangjieThird;
	boolean isTraditionToSimplified;
	int forcesRunFullscreen;
	int defaultPickupMaxSize=36;
	boolean isKeyboardFiveRowMode;
	int keyboardKeySizeMode;
	int mixInputDisplayMode;
	int cangjieInputKeyCodeDisplayPosition;
	
	static public Preferences loadPreferences(Context context){
		if (currentPrefObj == null){
			currentPrefObj = new Preferences(context);
		}
		return currentPrefObj;
	}
	
	static public void unloadPreferences(){
		if (currentPrefObj != null){
			currentPrefObj.close();
			currentPrefObj = null;
		}
	}
	
	private Preferences(Context context){
		mPref = PreferenceManager.getDefaultSharedPreferences(context);
		mPref.registerOnSharedPreferenceChangeListener(this);
		loadSetting();
	}
	
	public void loadSetting(){
		defaultPickupMode = Integer.valueOf(mPref.getString(PREF_DEFAULT_PICKUP_MODE, "3"));
		enableKeySound = mPref.getBoolean(PREF_ENABLE_KEY_SOUND, true);
		enableKeyVibrate = mPref.getBoolean(PREF_ENABLE_KEY_VIBRATE, true);
		enableRelatedChineseWord = mPref.getBoolean(PREF_ENABLE_RELATED_CHINESE_WORD, true);
		enableCangjieAutoCorrect = mPref.getBoolean(PREF_ENABLE_CANGJIE_AUTO_CORRECT, true);
		isSymbolsPickupChinese = mPref.getBoolean(PREF_IS_SYMBOLS_PICKUP_CHINESE, true);
		isSupportCangjieThird = mPref.getBoolean(PREF_SUPPORT_CANGJIE_THIRD, false);
		isTraditionToSimplified = mPref.getBoolean(PREF_TRADITION_TO_SIMPLIFIED, false);
		forcesRunFullscreen = Integer.valueOf(mPref.getString(PREF_FORCES_RUN_FULLSCREEN, "0"));
		mixInputDisplayMode = Integer.valueOf(mPref.getString(PREF_MIX_INPUT_DISPLAY_MODE,"0"));
		if(mixInputDisplayMode != MID_ENGLISH_FIRST){
			isSymbolsPickupChinese = true;
		}
		
		cangjieInputKeyCodeDisplayPosition = Integer.valueOf(mPref.getString(PREF_CANGJIE_INPUT_KEY_CODE_POSITION, "3"));
		
		int oldKeyboardKeySizeMode = keyboardKeySizeMode;
		keyboardKeySizeMode = Integer.valueOf(mPref.getString(PREF_KEYBOARD_KEY_SIZE_MODE, "0"));
		if (oldKeyboardKeySizeMode != keyboardKeySizeMode){
			keyboardPrefChanged();
		}
		
		
		boolean oldKeyboardFiveRowMode = isKeyboardFiveRowMode;
		isKeyboardFiveRowMode = mPref.getBoolean(PREF_KEYBOARD_FIVE_ROW_MODE, false);
		if (oldKeyboardFiveRowMode != isKeyboardFiveRowMode){
			keyboardPrefChanged();
		}
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		loadSetting();
	}
	
	public void close(){
		mPref.unregisterOnSharedPreferenceChangeListener(currentPrefObj);
	}
	
	private void keyboardPrefChanged(){
		for (IKeyboardPrefChangedListener l : mKeyboardPrefChangedListeners){
			l.keyboardPrefChanged();			
		}
	}
	
	public void registerKeyboardPrefChangedListener(IKeyboardPrefChangedListener listener){
		if (listener != null){
			mKeyboardPrefChangedListeners.add(listener);
		}
	}
	
	public void unregisterKeyboardPrefChangedListener(IKeyboardPrefChangedListener listener){
		mKeyboardPrefChangedListeners.remove(listener);
	}
}
